Practical 5 - Full Version

Table of Contents

Creating Mugger Agents
Criminal Behaviour
Activity
print

Practical 5 - Agents and Interactions


In this practical, the festival model will be extended by adding a new type of agent to represent criminals (called 'muggers') and by creating some interactions between different agents if they happen to bump into each other.

    Creating Mugger Agents

  1. Make a copy of your model from last week ('festival.nlogo') that you will use for this practical work. Give it a name so you can distinguish it from last week's work (e.g. I called mine 'festival2.nlogo').
  2. Open the model and start editing the code.
  3. Create a new type of agent that will be referred to as 'muggers' (or 'mugger' if there is only one of them). Hint: you will need to add a new breed command - if you can't remember how this command works check the NetLogo Programming Guide or refer to the last practical.
  4. Create a new variable called 'victims' that the muggers will use to keep track of the number of people they mug. Hint: the muggers-own command will do this, as covered in the last practical.
  5. Now, NetLogo knows that there are two types of agents in the model. The next thing to do is actually create some muggers

  6. In the interface tab, create a new slider called 'number-of-muggers'. This will be used to set the number of mugger agents. You can choose the limits of the slider - I choose a limit of 100 and default of 10.
  7. code in the setup procedures
  8. Back in the code tab, create a new procedure called 'setup-muggers'. This procedure should create the new mugger agents and do three things:
    1. Set their shape. This could be "person", like the visitors, or something else if you choose - see the NetLogo shape editor guide for other shapes.
    2. Set their colour. Choose something so that you can tell the difference between 'visitors' and 'muggers' but don't choose a colour that is already being used by patches (if you do this then when a mugger moves onto a patch of the same colour it will be camouflaged and invisible).
    3. Move them to a randomly chosen position.
    If you forget how to do any of the above, check back to the last practical.
  9. Add a call to your 'setup-muggers' procedure in the main 'setup' procedure, just after the call to 'setup-people. By the end, your setup procedures will look similar to the right image. Sorry, the setup-muggers procedure has gotten smudged, you'll have to work out what goes in there on your own!
  10. Check that your code works by going to the interface tab and setting up the model. You should see some muggers alongside the visitors, although if you press 'go' the muggers wont do anything. Yet.
  11. Criminal Behaviour

    The next stage is to implement some behaviour for the mugger agents. We need a procedure that will move the muggers around the environment and, if they come into contact with a visitor, commit a mugging.

  12. First, in the Interface tab, create a switch called show-crimes. This will be used later to turn on (or off) a label that shows where crimes have been committed.
  13. Next, we will create a new procedure that controls the behaviour of the mugger agents. There is already a procedure called 'move' that tells the visitor agents what to do at each model iteration. No we need a similar procedure that will tell a mugger to walk around the festival site and, if they come across a visitor, mug them.

  14. Create a new procedure called 'move-and-mug'. This procedure will contain the code that we need to control the mugger agents.
  15. Add the code from the table below to your new procedure. Make sure you understand what each command is doing, and ask someone if you're confused!
  16. CodeExplanation
    to move-and-mug The procedure is called 'move-and-mug'. It will control where the agents go and make them mug visitors if they happen to meet on the same patch.
    rt (random 360)
    fd 1
    These commands make the muggers move around the environment randomly. They don't move between food stalls and stages like the visitors do.
    if count visitors-here > 0 [ Important! This commands checks to see if there are any visitors on the same patch as the mugger. If there is at least one visitor on the patch, then any code after the square bracket will run.
    set victims ( victims + 1 ) This code will only be executed if there are victims here. It increases the value of the mugger's victims variable by 1 unit. This tells us that a mugging has taken place.
    if show-crimes = True [ The last thing we want to do, after a mugging, is (optionally) to show where crimes have taken place. This command checks to see if the show-crimes switch is set to 'on'.
    ask patch-here [ Ask the patch where the mugging has just taken place ...
    set plabel-color black ... to create a label that has black text ...
    set plabel "C" ... and shows the letter 'c' (as in 'crime').
      ]
    ]
    Closing braces for the if show-crimes = True and ask patch-here commands.
    ] This closing bracket closes the count visitors-here > 0 statement above.
    end Finish the move-and-mug procedure.
  17. Almost there. Now, to ask each mugger to move you have to add the following to the 'go' procedure after the visitors have moved: ask muggers [
      move-and-mug
    ]
  18. mugging graph
  19. That's it! Now go to the interface and try to run the model. If it works you should see muggers and visitors walking around. If you turn the show-crimes switch on then little 'C' characters will appear each time a mugger and visitor come into contact.
  20. The very last thing to do is make a graph that shows the rate of muggings (i.e. the number of muggings per model iteration). To do this, create a new plot with values similar to the right image. The code required to plot the number of muggings per iteration is: plot ( sum [ victims ] of muggers ) / ( ticks + 1) (note: ticks+1 is used to prevent problems with dividing by zero when the model starts).

When you have finished, your model should look something similar to the one below. There is quite a lot to take in from this practical. Before you finish, have a go at the activity below. It will help you to understand how the behaviour of the agents really works.

The final model for practical 5
The festival site

Activity

Change the behaviour of the mugger agents so that, instead of walking randomly around the festival site they move between the two stages.

Hints:

What affect does this change in behaviour have on the number of muggings in your model and their distribution?